package cn.szzsi.platform.controller.web;

import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.core.kit.MD5;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by admin on 2017/7/15.
 */
public class TestEncrypt extends Controller{

    public void index(){
        render(CommonUtil.TEMPLATE_PATH + "/platform/encrypt/encrypt_input.html");
    }

    public void sign(){
        String jsonData=getPara("jsonData");
        String time_stamp=getPara("time_stamp");
        String secret=getPara("secret");
        JSONObject jsonObject=null;
        try{
            jsonObject=new JSONObject(jsonData);
        }catch (Exception e){
            renderJson(Ret.fail("info","签名失败,请检查json数据是否含有非法字符!"));
            return;
        }

        Iterator ite = jsonObject.keys();
        // 遍历jsonObject数据,添加到Map对象
        Map data=new HashMap();
        while (ite.hasNext()) {
            String key = ite.next().toString();
            String value = jsonObject.get(key).toString();
            data.put(key, value);
        }
        data.put("time_stamp",time_stamp);
        String sortMap= MD5.getSortStr("",data);
        String signature= MD5.sign(sortMap,"&secret="+secret,"utf-8");//服务器签名
        String signatureUp=signature.toUpperCase();
        renderJson(Ret.ok().set("signature",signature).set("signatureUp",signatureUp));

    }

    public static void main(String[] args) {
        System.out.println(Ret.fail("info","签名失败,请检查json数据是否含有非法字符!"));
    }
}
